<?php
/**
 * JoiGallery - image gallery component for Joostina CMS
 *
 * Backend install handler
 *
 * @version 1.0 alpha 3
 * @package JoiGallery
 * @subpackage	Installer
 * @filename install.joigallery.php
 * @author JoostinaTeam
 * @copyright (C) 2008-2009 Joostina Team
 * @license see license.txt
 *
 **/

defined('_VALID_MOS') or die();

function com_install() {

	$mainframe = &mosMainFrame::getInstance();
	$database = &database::getInstance();

	//Создание директорий
	$dirs = array('joigallery', 'joigallery/thumbs', 'joigallery/resized');
	foreach($dirs as $dir) {
		joi_check_dir($mainframe->getCfg('absolute_path').'/images/'.$dir);
	}

	//Распаковка архивов
	$zipfile = $mainframe->getCfg('absolute_path').DS.'components'.DS.'com_joivideo'.DS.'view'.DS.'front_view.zip';
	$unzip_dir = $mainframe->getCfg('absolute_path').DS.'components'.DS.'com_joivideo'.DS.'view';
	if(!joi_unzip($zipfile, $unzip_dir)) {
		return false;
	}

	$zipfile = $mainframe->getCfg('absolute_path').DS.'administrator'.DS.'components'.DS.'com_joivideo'.DS.'view'.DS.'admin_view.zip';
	$unzip_dir = $mainframe->getCfg('absolute_path').DS.'administrator'.DS.'components'.DS.'com_joivideo'.DS.'view';
	if(!joi_unzip($zipfile, $unzip_dir)) {
		return false;
	}

	$zipfile = $mainframe->getCfg('absolute_path').DS.'components'.DS.'com_joivideo'.DS.'language'.DS.'language.zip';
	$unzip_dir = $mainframe->getCfg('absolute_path').DS.'components'.DS.'com_joivideo'.DS.'language';
	if(!joi_unzip($zipfile, $unzip_dir)) {
		return false;
	}

	$zipfile = $mainframe->getCfg('absolute_path').DS.'components'.DS.'com_joivideo'.DS.'lib'.DS.'lib.zip';
	$unzip_dir = $mainframe->getCfg('absolute_path').DS.'components'.DS.'com_joivideo'.DS.'lib';
	if(!joi_unzip($zipfile, $unzip_dir)) {
		return false;
	}

	$zipfile = $mainframe->getCfg('absolute_path').DS.'components'.DS.'com_joivideo'.DS.'plugins'.DS.'plugins.zip';
	$unzip_dir = $mainframe->getCfg('absolute_path').DS.'components'.DS.'com_joivideo'.DS.'plugins';
	if(!joi_unzip($zipfile, $unzip_dir)) {
		return false;
	}
    
	$zipfile = $mainframe->getCfg('absolute_path').DS.'components'.DS.'com_joivideo'.DS.'watermark.zip';
	$unzip_dir = $mainframe->getCfg('absolute_path').'/images/watermark';
	if(!joi_unzip($zipfile, $unzip_dir)) {
		return false;	}


}

function joi_check_dir($dir) {

	if (!is_dir($dir)) {
		umask(011);
		if (!mkdir($dir)) {
			echo 'Ошибка установки: невозможно создать директорию '.$dir;
			return false;
		}
		mosChmod($dir, 0777);
		return true;
	}
	return true;
}

function joi_unzip($zip, $unzip_dir) {
	$mainframe = &mosMainFrame::getInstance();


	require_once ($mainframe->getCfg('absolute_path') . '/administrator/includes/pcl/pclzip.lib.php');
	require_once ($mainframe->getCfg('absolute_path') . '/administrator/includes/pcl/pclerror.lib.php');

	$zipfile = new PclZip($zip);

	$ret = $zipfile->extract(PCLZIP_OPT_PATH, $unzip_dir);
	if ($ret == 0) {
		echo "Unrecoverable error: " . $zipfile->errorName(true).'<br />';
		return false;
	}
	unlink($zip);
	return true;
}